package com.shanghai.shanghaielectric.UI;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.DataAdapter.Cells2ShowAdapter;
import com.shanghai.shanghaielectric.Model.ReceiveDataModel;
import com.shanghai.shanghaielectric.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import shanghaielectric_lib.MyEventBus.EventBus_showCells;
import shanghaielectric_lib.RDBHelper.Cell_show;


/**
 * 监测-单元界面
 */
public class MonitorUnitFragment extends Fragment {


    public View monitorUnitView;
    @BindView(R.id.tv_unit_id)
    TextView tvUnitId;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.system_state)
    TextView systemState;
    @BindView(R.id.positive_voltage)
    TextView positiveVoltage;
    @BindView(R.id.negative_voltage)
    TextView negativeVoltage;
    @BindView(R.id.tv_station_id)
    TextView tvStationId;
    @BindView(R.id.tv_capacity)
    TextView tvCapacity;
    @BindView(R.id.view_light_unit)
    View viewLightUnit;
    @BindView(R.id.igbt_tem_a)
    TextView igbtTemA;
    @BindView(R.id.igbt_tem_b)
    TextView igbtTemB;
    @BindView(R.id.igbt_tem_c)
    TextView igbtTemC;
    @BindView(R.id.tv_errorCode_1)
    TextView tvErrorCode1;
    @BindView(R.id.tv_errorCode_2)
    TextView tvErrorCode2;
    @BindView(R.id.tv_errorCode_3)
    TextView tvErrorCode3;
    @BindView(R.id.tv_errorCode_4)
    TextView tvErrorCode4;
    @BindView(R.id.tv_errorCode_5)
    TextView tvErrorCode5;
    @BindView(R.id.tv_alertCode_1)
    TextView tvAlertCode1;
    @BindView(R.id.tv_alertCode_2)
    TextView tvAlertCode2;
    @BindView(R.id.tv_alertCode_3)
    TextView tvAlertCode3;
    @BindView(R.id.tv_alertCode_4)
    TextView tvAlertCode4;
    Unbinder unbinder;

    Cells2ShowAdapter cells2ShowAdapter;
    private int currentDeviceID = 1;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("MonitorUnit____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("MonitorUnit____onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        System.out.println("MonitorUnit____onCreateView");
        monitorUnitView = inflater.inflate(R.layout.monitor_unit_fragment,
                container, false);


        unbinder = ButterKnife.bind(this, monitorUnitView);
        initView();
        initData();
        intListener();
        EventBus.getDefault().register(this);


        return monitorUnitView;
    }

    private void initData() {
        cells2ShowAdapter = new Cells2ShowAdapter();
    }

    private void intListener() {


    }

    public void initView() {


    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("MonitorUnit____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("MonitorUnit____onStart");
    }

    @Override
    public void onResume() {//
        super.onResume();


        System.out.println("MonitorUnit____onResume" + Constants.CURRENT_PRESSED_UNIT);
    }

    @Override
    public void onPause() {
        super.onPause();
        System.out.println("MonitorUnit____onPause");
    }

    public void setCurrentDeviceID(int deviceID){
        currentDeviceID=deviceID;

    }
    @Override

    public void onStop() {
        super.onStop();
        System.out.println("MonitorUnit____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("MonitorUnit____onDestroyView");
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("MonitorUnit____onDestroy");
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("MonitorUnit____onDetach");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveCellsData(EventBus_showCells eventBusShowCells) {
        Map<Integer, Cell_show> cell_showMap = eventBusShowCells.cell_showMap;
        if (eventBusShowCells.deviceID == currentDeviceID) {
            tvUnitId.setText("单元"+currentDeviceID);
            ReceiveDataModel receiveDataModel = cells2ShowAdapter.cells2ReceiveDataModel(eventBusShowCells.cell_showMap);
            systemState.setText(receiveDataModel.systemState);
            positiveVoltage.setText(receiveDataModel.UdcP);
            negativeVoltage.setText(receiveDataModel.UdcN);
            tvStationId.setText(receiveDataModel.stationID);
            tvCapacity.setText(receiveDataModel.capacity);

            int resourceID = receiveDataModel.unite_FJ_state ? R.drawable.point_green : R.drawable.point_gray;
            viewLightUnit.setBackgroundResource(resourceID);

            igbtTemA.setText(receiveDataModel.IGBT_A);
            igbtTemB.setText(receiveDataModel.IGBT_B);
            igbtTemC.setText(receiveDataModel.IGBT_C);

            tvErrorCode1.setText(receiveDataModel.errorCode_1);
            tvErrorCode2.setText(receiveDataModel.errorCode_2);
            tvErrorCode3.setText(receiveDataModel.errorCode_3);
            tvErrorCode4.setText(receiveDataModel.errorCode_4);
            tvErrorCode5.setText(receiveDataModel.errorCode_5);

            tvAlertCode1.setText(receiveDataModel.alertCode_1);
            tvAlertCode2.setText(receiveDataModel.alertCode_2);
            tvAlertCode3.setText(receiveDataModel.alertCode_3);
            tvAlertCode4.setText(receiveDataModel.alertCode_4);

        }
    }

}
